<template>
  <div style="background-color: #f6f6f6; min-height: 100vh">
    <div class="front-notice">
      <i class="el-icon-bell" style="margin-right: 2px"></i>公告：{{ top }}
    </div>
    <!--头部-->
    <div class="front-header">
      <div class="front-header-left">
        <img src="@/assets/imgs/logo.png" alt="" />
        <div class="title">社区养老系统</div>
      </div>
      <div class="front-header-center">
        <div class="front-header-nav">
          <el-menu :default-active="$route.path" mode="horizontal" router>
            <el-menu-item index="/front/home">首页</el-menu-item>
            <el-menu-item index="/front/activity">公益活动</el-menu-item>
            <el-menu-item index="/front/serve">养老服务</el-menu-item>
            <el-menu-item index="/front/chat">在线论坛</el-menu-item>
            <el-menu-item index="/front/nurseHouse">养老院</el-menu-item>
            <el-menu-item index="/front/person">个人中心</el-menu-item>
          </el-menu>
        </div>
      </div>
      <div class="front-header-right">
        <div v-if="!user.username">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown>
            <div class="front-header-dropdown">
              <img :src="user.avatar" alt="" />
              <div style="margin-left: 10px">
                <span>{{ user.name }}</span
                ><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
              </div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div @click="$router.push('/front/activitySign')">
                  我报名的活动
                </div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click="$router.push('/front/reserve')">我预约的服务</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click="logout">退出登录</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!--主体-->
    <div class="main-body">
      <router-view ref="child" @update:user="updateUser" />
    </div>

    <Footer />
  </div>
</template>

<script>
import Footer from "@/components/Footer";
export default {
  name: "FrontLayout",
  components: { Footer },
  data() {
    return {
      top: "",
      notice: [],
      user: JSON.parse(localStorage.getItem("xm-user") || "{}"),
    };
  },

  mounted() {
    this.loadNotice();
  },
  methods: {
    loadNotice() {
      this.$request.get("/notice/selectAll").then((res) => {
        this.notice = res.data;
        // 添加个人信息的提示
        this.$request({
          url: "/bodydata/selectAll",
          method: "GET",
        }).then((user) => {
          let chartData = user.data.filter((v) => v.userId === this.user.id);
          // 分析体重
          if (chartData.length) {
            const totalChange =
              chartData[chartData.length - 1].weight - chartData[0].weight;
            const averageDailyChange = totalChange / (chartData.length - 1);
            if (totalChange > 0 && averageDailyChange > 0) {
              this.notice.push({
                content: "您最近体重持续增加，请注意观察",
              });
            } else if (totalChange < 0 && averageDailyChange < 0) {
              this.notice.push({
                content: "您最近体重持续降低，请注意营养补充",
              });
            }
            // 分析血糖
            let lastSevenData = chartData.slice(-7);
            if (lastSevenData.some((v) => v.sugar > 7.8)) {
              this.notice.push({
                content: "您最近血糖异常，请注意观察",
              });
            }
            // 分析血压
            if (
              lastSevenData.some(
                (v) => v.pressurelow < 60 || v.pressurelow > 80
              )
            ) {
              this.notice.push({
                content: "您最近低压异常，请注意观察",
              });
            }
            if (lastSevenData.some((v) => v.pressurehigh > 120)) {
              this.notice.push({
                content: "您最近高压异常，请注意观察",
              });
            }
          }

          let i = 0;
          if (this.notice && this.notice.length) {
            this.top = this.notice[0].content;
            setInterval(() => {
              this.top = this.notice[i].content;
              i++;
              if (i === this.notice.length) {
                i = 0;
              }
            }, 5000);
          }
        });
      });
    },
    updateUser() {
      this.user = JSON.parse(localStorage.getItem("xm-user") || "{}"); // 重新获取下用户的最新信息
    },
    // 退出登录
    logout() {
      localStorage.removeItem("xm-user");
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
@import "@/assets/css/front.css";
</style>
